package com.cn.springcloudwechat.service.personalcenter.serviceImpl;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.common.weiXinPay.weiXinSina.Md5Util;
import com.cn.springcloudwechat.mapper.mapper.YmUserMapper;
import com.cn.springcloudwechat.mapper.mapper.YmUserPointMapper;
import com.cn.springcloudwechat.model.YmChildGrowth;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserPoint;
import com.cn.springcloudwechat.model.weixin.user.WxFollower;
import com.cn.springcloudwechat.redis.RedisUtil;
import com.cn.springcloudwechat.service.YmAnswerService;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service("YmUserService")
public class YmUserServiceImpl implements YmUserService{

    @Autowired
    YmUserMapper ymUserMapper;

    @Autowired
    YmUserPointMapper ymUserPointMapper;


    @Override
    public YmUser selectByPrimaryKey(Long userId) {
        return ymUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public YmUser getUserByOpenId(String openid) {
        Assert.notNull(openid,String.format(ErrorUtil.PARAMETER_NULL,openid));
        //尝试从redis服务器获取用户
       YmUser ymUser = (YmUser) RedisUtil.getInstace().get(openid+"user");
       if(ymUser == null){
           ymUser = ymUserMapper.getUserByOpenId(openid);
           if(ymUser != null){
               RedisUtil.getInstace().del(openid+"user");
               RedisUtil.getInstace().set(openid+"user",ymUser);
           }
       }
       return ymUser;
    }

    @Override
    public YmUser getUserByPhone(String phone) {
        Assert.notNull(phone,String.format(ErrorUtil.PARAMETER_NULL,phone));
        return ymUserMapper.getUserByPhone(phone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertUserByWx(WxFollower wxFollower, String password, String userPhone) {
        YmUser ymUser = new YmUser();
        if(!StringUtils.isEmpty(password)){
            String md5 = Md5Util.md5(password);
            ymUser.setPassword(md5);
        }
        if (!StringUtils.isEmpty(userPhone)) {
            ymUser.setUserPhone(userPhone);
        }
        ymUser.setRegisterChannelId(wxFollower.getOpenid());
        ymUser.setRegisterChannel("微信");
        ymUser.setHeadimgurl(wxFollower.getHeadimgurl());
        ymUser.setNickname(wxFollower.getNickname());
        ymUser.setType((short) 0);
        ymUser.setAddress(wxFollower.getCountry()+wxFollower.getProvince()+wxFollower.getCity());
        return ymUserMapper.insertSelective(ymUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateUser(YmUser ymUser) {
        Assert.notNull(ymUser,String.format(ErrorUtil.PARAMETER_NULL,"ymUser"));
        Integer result = ymUserMapper.updateByPrimaryKeySelective(ymUser);
        if(result != 0){
            //修改redis存储的用户信息
            YmUser user = ymUserMapper.selectByPrimaryKey(ymUser.getUserId());
            RedisUtil.getInstace().del(user.getRegisterChannelId()+"user");
            RedisUtil.getInstace().set(user.getRegisterChannelId()+"user",user);
            //修改session
            ThreadLocalUtil.getRequest().getSession().removeAttribute("user");
            ThreadLocalUtil.getRequest().getSession().setAttribute("user",user);
        }
        return result;
    }

    @Override
    public Page<YmUser> getUserList(YmUser ymUser, Page<YmUser> page) {
        List<YmUser> ymUserList = ymUserMapper.getUserList(ymUser,page);
        page.setResults(ymUserList);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserState(Integer state,String[] userId) {
        ymUserMapper.updateUserState(state,userId);
    }

    @Override
    public Page<YmUserPoint> getPoint(Long userId,Page<YmUserPoint> page) {
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        page.setResults(ymUserPointMapper.getPoint(userId,page));
        return page;
    }
}
